package com.finger.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.commonutils.CourseWebVoOrder;
import com.finger.commonutils.R;
import com.finger.eduservice.entity.EduCourse;
import com.finger.eduservice.entity.frontvo.CourseWebVo;
import com.finger.eduservice.entity.frontvo.QueryCourseFront;
import com.finger.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RequestMapping("/eduservice/coursefront")
@RestController
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @PostMapping("/getCourseList/{page}/{size}")
    public R getCourseList(@PathVariable long page,
                           @PathVariable long size,
                           @RequestBody(required = false) QueryCourseFront queryCourseFront){

        Page<EduCourse> eduCoursePage = new Page<>(page,size);

       Map<String,Object> map = eduCourseService.getCourseListCondition(eduCoursePage,queryCourseFront);

       return R.ok().data("data",map);
    }

    @GetMapping("/getCourseInfo")
    public CourseWebVoOrder getCourseInfo(@RequestParam("id") String id){

        CourseWebVo courseWebVo = eduCourseService.getCourseWebVoById(id);

        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();

        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);

        return courseWebVoOrder;

    }

    /**
     * 更新课程购买数量
     * @param courseId
     * @return
     */
    @PutMapping("/updateCourseBuyCount")
    public boolean updateCourseBuyCount(@RequestParam("courseId")String courseId){
        try {
            eduCourseService.updateCourseBuyCount(courseId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
